package com.ce.pms.house.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.*;

import org.hibernate.annotations.Where;

import com.ce.pms.common.model.SysInfo;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.sitechasia.webx.core.model.IDomainObject;
/**
 * 实体:管理房
 * @function
 * @author Adama Yue
 * @CreateDate:2008-7-25
 */
@Entity
@Table(name="HM_MANAGE")
@PrimaryKeyJoinColumn(name="MANAGE_ID")
public class CoalitionHouseDomain extends HouseDomain {
	/**
	 * 解除管理时间
	 */
	@Temporal(TemporalType.DATE)
	@Column(name="DISUNIT_DATE")
	private Date disUnitDate;

	/**
	 * 合并关系
	 */
	@ManyToOne
	@JoinColumn(name = "SYS_REF_ID")
	private SysrefcodeDO coalitionType;

	/**
	 * 关联房屋集合 检索策略:lazy=true
	 */
	@OneToMany
	@Where(clause="IS_VIRTUAL = '0'")
	@org.hibernate.annotations.Cascade(value = { org.hibernate.annotations.CascadeType.SAVE_UPDATE })
	@JoinTable(name = "HM_REAL_R_VIRTUAL", joinColumns = { @JoinColumn(name = "VIRTUAL_HOUSE_ID",unique=true) }, inverseJoinColumns = { @JoinColumn(name = "REAL_HOUSE_ID") })
	private Set<HouseDomain> houses = new HashSet<HouseDomain>();

	/**
	 * 是否可用
	 */
	@Column(name="VALIDITY",nullable=false)
	private String validity;

    public CoalitionHouseDomain(){
        super();
    }


	/**
	 * mini constructor
	 * @param houseNumber  管理房号
	 * @param coalitionType管理房类型
	 * @param houses       房屋集合
	 * @param coalitionType管理房类型
	 * @param sysInfo      系统信息
	 */
	public CoalitionHouseDomain(String houseNumber, Set<HouseDomain> houses,SysrefcodeDO coalitionType, SysInfo sysInfo) {
		super(houseNumber, SysrefcodeDO.CONSTANTS_DIGITAL_ONE, sysInfo);
		this.houses = houses;
		this.coalitionType = coalitionType;
		this.validity = SysrefcodeDO.CONSTANTS_DIGITAL_ONE;
	}
	/**************************************************
	 *Bussiness method
	 **************************************************/
	public void generatedCoalitionNumber() {

		if(!SysrefcodeDO.CS_COALITION_HOUSE_RELATION_TYPE_MERGE.equals(
				 this.getCoalitionType().getValue())) {
			//产生管理房号
			StringBuffer houseNO = new StringBuffer();
			for(HouseDomain h:this.getHouses()) {
				houseNO.append(h.getHouseNumber());
				houseNO.append(",");
			}
			houseNO.setLength(houseNO.length() - 1);
			setHouseNumber( houseNO.toString());
		}
	}
	/**
	 * 一个管理房至少要包含两个房间
	 *
	 * @author Adama Yue
	 * @CreateDate:上午10:11:52
	 */
	public void checkHousesMoreThanTwo() {
		if(this.getHouses().size()<2)
		throw new BusinessException("house:house_coalition_housesmorethantwo");
	}

	/***************************************************
	 * accessors and modifiers
	 ***************************************************/
	public Date getDisUnitDate() {
		return disUnitDate;
	}

	public void setDisUnitDate(Date disUnitDate) {
		this.disUnitDate = disUnitDate;
	}

	public String getValidity() {
		return validity;
	}

	public void setValidity(String validity) {
		this.validity = validity;
	}

	public SysrefcodeDO getCoalitionType() {
		return coalitionType;
	}

	public void setCoalitionType(SysrefcodeDO coalitionType) {
		this.coalitionType = coalitionType;
	}

	public Set<HouseDomain> getHouses() {
		return houses;
	}

	public void setHouses(Set<HouseDomain> houses) {
		this.houses = houses;
	}

}
